/**
 * 浅比较两个object, json的key是否一致
 * @param obj1
 * @param obj2
 * @returns
 */
export function equalObjectKey(obj1: Object, obj2: Object): boolean {
  const obj1Keys: string[] = Object.keys(obj1)
  const obj2Keys: string[] = Object.keys(obj2)
  const obj1KeysLen: number = obj1Keys.length
  if (obj1KeysLen !== obj2Keys.length) {
    return false
  }
  let is = true
  for (let index = 0; index < obj1KeysLen; index++) {
    const element: string = obj1Keys[index]
    if (!Object.prototype.hasOwnProperty.call(obj2, element)) {
      is = false
      break
    }
  }
  return is
}

/**
 * 浅比较两个对象是否相等，这两个对象的值只能是数字或字符串
 * @param obj1
 * @param obj2
 * @returns
 */
export function equalObject(obj1: Object, obj2: Object): boolean {
  const obj1Keys: string[] = Object.keys(obj1)
  const obj2Keys: string[] = Object.keys(obj2)
  const obj1KeysLen: number = obj1Keys.length
  const obj2KeysLen: number = obj2Keys.length
  if (obj1KeysLen !== obj2KeysLen) {
    return false
  }

  if (obj1KeysLen === 0 && obj2KeysLen === 0) {
    return true
  }

  return !obj1Keys.some((key) => obj1[key] != obj2[key])
}

/**
 * 深拷贝两个对象
 * @param obj
 * @param map
 * @returns
 */
export const deepClone = (obj, map = new WeakMap()) => {
  if (obj == null) return obj
  if (obj instanceof Date) return new Date(obj)
  if (obj instanceof RegExp) return new RegExp(obj)
  if (typeof obj !== 'object') return obj
  if (map.get(obj)) return map.get(obj)
  const cloneObj = new obj.constructor()
  map.set(obj, cloneObj)
  for (const key in obj) {
    if (obj.hasOwnProperty(key)) {
      cloneObj[key] = deepClone(obj[key], map)
    }
  }
  return cloneObj
}
